package com.ghostflying.groundstation;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class PopupActivity extends Activity {
	
	private SeekBar high_input = null;
	private Button delete_fly = null;
	private Button insert_fly = null;
	private Button upload_fly = null;
	private Button cancel = null;
	private TextView high_now = null;
	private TextView popup_title = null;
	private Intent main;
	public static final int RESULT_NONE = 0;
	public static final int RESULT_CANCEL = 1;
	public static final int RESULT_DELETE = 2;
	public static final int RESULT_INSERT = 3;
	public static final int RESULT_UPLOAD = 4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_popup);
		
		high_input = (SeekBar)findViewById(R.id.high_input);
		delete_fly = (Button)findViewById(R.id.delete_fly);
		insert_fly = (Button)findViewById(R.id.insert_fly);
		upload_fly = (Button)findViewById(R.id.upload_fly);
		cancel = (Button)findViewById(R.id.cancel);
		high_now = (TextView)findViewById(R.id.high_now);
		popup_title = (TextView)findViewById(R.id.popup_title);
		popup_title.setText(getString(R.string.popup_title) + Integer.toString((Integer) getIntent().getExtras().get("position")));
		main = new Intent ();
		high_input.setProgress((Integer) getIntent().getExtras().get("high"));
		high_now.setText(getString(R.string.high_now_pre)+String.valueOf(high_input.getProgress())+getString(R.string.high_now_last));
		
		delete_fly.setOnClickListener(click_delete_fly);
		insert_fly.setOnClickListener(click_insert_fly);		
		high_input.setOnSeekBarChangeListener(high_change);
		upload_fly.setOnClickListener(click_upload_fly);
		cancel.setOnClickListener(click_cancel);
	}
	
	OnClickListener click_upload_fly = new OnClickListener(){
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			set_high();
			setResult(RESULT_UPLOAD,main);
			finish();
		}
		
	};
	
	OnClickListener click_cancel = new OnClickListener(){
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			setResult(RESULT_CANCEL);
			finish();
		}
		
	};
	OnClickListener click_delete_fly = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			set_high();
			setResult(RESULT_DELETE);
			finish();
		}
		
	};
	
	OnClickListener click_insert_fly = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			set_high();
			setResult(RESULT_INSERT,main);
			finish();
		}
		
	};
	
	OnSeekBarChangeListener high_change = new OnSeekBarChangeListener (){

		@Override
		public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
			// TODO Auto-generated method stub
			high_now.setText(getString(R.string.high_now_pre)+String.valueOf(high_input.getProgress())+getString(R.string.high_now_last));
		}

		@Override
		public void onStartTrackingTouch(SeekBar arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar arg0) {
			// TODO Auto-generated method stub
			
		}
		
	};
	
	private void set_high (){
		main.putExtra("high",high_input.getProgress());		
		setResult(RESULT_NONE,main);			
	}
	
	public boolean onTouchEvent(MotionEvent event){
		set_high ();
		finish();
		return true;
	
	}



}
